home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / 13_PCK.C next >
Encoding:
C/C++ Source or Header  |  1995-06-25  |  7.5 KB  |  356 lines

  1. #if 0
  2. // C version works just fine but SLOW, kept it so you can tell
  3. //   what the asm version does
  4. /* ----------------- put_blit_packed() --------------------- March 23,1993 */
  5. void put_blit_packed(char far *buffer, short x, short y, short width, short height)
  6. {
  7.    unsigned char num;
  8.    char far *vid;
  9.    unsigned short i;
  10.  
  11.    i=y*320 + x;
  12.    vid=MK_FP(0xa000, i);
  13.    i=320 - width;
  14.  
  15.    for ( y=0; y < height; y++ )
  16.       {
  17.       x=0;
  18.       while ( x < width )
  19.          {
  20.          if ( *buffer )
  21.             {
  22.             buffer++;
  23.             num=*buffer++;
  24.             while ( num-- )
  25.                {
  26.                *vid++=*buffer++;
  27.                x++;
  28.                }
  29.             }
  30.          else
  31.             {
  32.             buffer++;
  33.             num=*buffer++;
  34.             vid+=num;
  35.             x+=num;
  36.             }
  37.          }
  38.  
  39.       vid+=i;
  40.       }
  41.  
  42.  
  43. }
  44. #endif
  45.  
  46. /*
  47.  
  48.    put a shape packed with pack shape
  49.    run length encoded
  50.    0 byte next byte is number of bytes to skip
  51.    1 byte next byte is number of bytes to draw
  52.  
  53.    only useful for TRANSPARENT SHAPES
  54.  
  55.    MAX width and height is 255!!!!!
  56.  
  57.    all color zero areas will be transparent see pack.c for more
  58.  
  59. */
  60. void put_blit_packed(FARPTR buffer, short x, short y, short width, short height)
  61. {
  62.  
  63.    asm   {
  64.          push  ds
  65.          push  di
  66.  
  67.          /* calc start address in video mem */
  68.          mov   ax, y
  69.          mov   bx, x
  70.          xchg  ah, al
  71.          add   bx, ax
  72.          shr   ax, 1
  73.          shr   ax, 1
  74.          add   bx, ax
  75.  
  76.          /* set up address registers for movsw */
  77.          mov   ax, 0xa000
  78.          mov   es, ax
  79.          mov   di, bx
  80.  
  81.          /* to make this draw to off-screen buffer add a dest parm
  82.          FARPTR dest, and replace the above 3 lines with
  83.  
  84.          les   di, dest
  85.          add,  di, bx
  86.  
  87.          see put_blit2() for an example
  88.  
  89.          */
  90.  
  91.          lds   si, buffer
  92.  
  93.          /* set up width and adjustment for fast blat */
  94.          mov   dx, width
  95.          mov   bx, 320
  96.          sub   bx, width
  97.          mov   ax, height
  98.          xor   ch, ch
  99.          }
  100.  
  101.          /* draw/skip each line */
  102. l1:
  103.    asm   {
  104.          xor   dh, dh
  105.          }
  106. lm0:
  107.    asm   {
  108.          cmp   byte ptr [si], 1
  109.          je    do_string
  110.  
  111. /*       skip black pixels */
  112.          inc   si
  113.          mov   cl, [si]
  114.          add   di, cx
  115.          add   dh, cl
  116.          inc   si
  117.  
  118.          /* check if done one line */
  119.          cmp   dh, dl
  120.          jne   lm0
  121.  
  122.          /* done one line, move to next */
  123.          add   di, bx
  124.          dec   ax
  125.          jnz   l1
  126.  
  127.          jmp   short the_end
  128.          }
  129.  
  130.          /*    draw a string of pixels */
  131. do_string:             
  132.    asm   {
  133.          inc   si
  134.  
  135.          mov   cl, [si]
  136.          add   dh, cl
  137.          inc   si
  138.  
  139.          rep   movsb
  140.  
  141.          /* check if done one line */
  142.          cmp   dh, dl
  143.          jne   lm0
  144.  
  145.  
  146.          /* done one line, move to next */
  147.          add   di, bx
  148.          dec   ax
  149.          jnz   l1
  150.          }
  151. the_end:
  152.          /* all done */
  153.    asm   {
  154.          pop   di
  155.          pop   ds
  156.          }   
  157. }
  158.  
  159.  
  160. /*
  161.  
  162.    FAST and easy mode 13 clipping
  163.  
  164.    buffer - pointer to shape
  165.    x, y   - where to put CLIPPED section of shape
  166.    full_width - full width of shape
  167.    dx, dy - where in shape to start blit
  168.    width, height - of clipped section we are drawing
  169.  
  170.  
  171.    shape
  172.  
  173.    |<-------- full_width----->|
  174.    ----------------------------
  175.    |   /\|                    |
  176.    |   dy|                    |
  177.    |   | |                    |
  178.    |<dx->|<--width------>|    |
  179.    |------------------------  |
  180.    |     |               | /\ |
  181.    |     |               | |  |
  182.    |     |               | |  |
  183.    |     |               | |  |
  184.    |     |  this rect is | height
  185.    |     |drawn at x, y  | |  |
  186.    |     | on the screen | |  |
  187.    |     |               | |  |
  188.    |     |               | \/ |
  189.    |     -------------------  |
  190.    |                          |
  191.    |                          |
  192.    ----------------------------
  193.  
  194.  
  195. */
  196. /* ---------------------- put_blit_clipped() ------------- April 24,1993 */
  197. void put_blit_clipped(FARPTR buffer, short x, short y, short full_width,
  198.                       short d_x, short d_y, short width, short height)
  199. {
  200.    unsigned short w;
  201.  
  202.    asm   {
  203.          push  ds
  204.          push  di
  205.  
  206.          /* calc w */
  207.          mov   ax, full_width
  208.          sub   ax, width
  209.          mov   w, ax
  210.  
  211.          /* calc start adrress in buffer */
  212.          lds   si, buffer
  213.          mov   ax, full_width
  214.          mul   word ptr d_y
  215.          add   ax, d_x
  216.          add   si, ax
  217.  
  218.          /* calc start address in vido mem */
  219.          mov   ax, y
  220.          mov   bx, x
  221.          xchg  ah, al
  222.          add   bx, ax
  223.          shr   ax, 1
  224.          shr   ax, 1
  225.          add   bx, ax
  226.  
  227.          /* set up address registers for movsb */
  228.          mov   ax, 0xa000
  229.          mov   es, ax
  230.          mov   di, bx
  231.  
  232.          /* set up width and adjustment for fast blat */
  233.          mov   dx, width
  234.          mov   bx, 320
  235.          sub   bx, width
  236.          mov   ax, height
  237.          }
  238.  
  239.          /* blast each line */
  240. l1:
  241.    asm   {
  242.          mov   cx, dx
  243.          rep   movsb
  244.          add   di, bx
  245.          add   si, w
  246.          dec   ax
  247.          jnz   l1
  248.  
  249.          /* all done */
  250.          pop   di
  251.          pop   ds
  252.          }   
  253. }
  254.  
  255.  
  256.  
  257.  
  258. /* ------------------------------ end of file ------------------------- */
  259.  
  260. ; start of pack.c
  261.  
  262. /*
  263.  
  264.    copyright 1993, Alec Russell, all rights reserved
  265.  
  266.    stuff to pack shapes - a utility
  267.  
  268. */
  269.  
  270.  
  271.  
  272. #include <mode13.h>
  273.  
  274.  
  275. /*
  276.  
  277.    pack a shape to speed up drawing TRANSPARENT shapes
  278.  
  279.    run length encoded
  280.    0 byte next byte is number of bytes to skip
  281.    1 byte next byte is number of bytes to draw
  282.  
  283.    only useful for TRANSPARENT SHAPES
  284.  
  285.    all color zero areas will be transparent
  286.  
  287.    MAX width and height is 255!!!!!
  288.  
  289.    packed line by line for speed
  290.  
  291.    this is NOT a good way to pack stuff if interested in saving space
  292.  
  293.    Once packed, use put_blit_packed() to draw.
  294.    Of course you will want to pack everthing in advance.
  295.    I usually put the function below into a stand-alone util and
  296.    pre-pack my sprites, saving them to disk packed.
  297.  
  298. */
  299. /* ---------------------- pack_shape() ------------------- April 15,1993 */
  300. void pack_shape(shape_t *shp1, shape_t *shp2)
  301. {
  302.    char far *s0, far *s1, far *t;
  303.    short num, x, y;
  304.  
  305.    s0=shp1->bitmap;
  306.    s1=shp2->bitmap;
  307.  
  308.    for ( y=0; y < shp1->height; y++ )
  309.       {
  310.       x=0;
  311.       while ( x < shp1->width )
  312.          {
  313.          if ( *s0 )
  314.             {
  315.             /* bytes isn't a zero so copy over until end of line or a zero */
  316.             num=0;
  317.             *s1++=1;
  318.             t=s1;    /* save where the count goes */
  319.             s1++;
  320.  
  321.             /* copy and count non-zero bytes */
  322.             while ( *s0 && x < shp1->width )
  323.                {
  324.                num++;
  325.                x++;
  326.                *s1++=*s0++;
  327.                }
  328.  
  329.             /* store count */
  330.             *t=num;
  331.             }
  332.          else
  333.             {
  334.             /* count number of zeros to skip and store count */
  335.             num=1;
  336.             *s1++=0;
  337.             s0++;
  338.             x++;
  339.  
  340.             while ( *s0 == 0 && x < shp1->width )
  341.                {
  342.                num++;
  343.                x++;
  344.                s0++;
  345.                }
  346.  
  347.             *s1++=num;
  348.             }
  349.          }
  350.       }
  351.  
  352.  
  353. }
  354.  
  355. /* -------------------------- end of file -------------------------- */
  356.